/*
 * @Author: your name
 * @Date: 2021-07-20 16:29:50
 * @LastEditTime: 2021-07-28 14:32:32
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \cuiyuan_activity\assets\script\Form.ts
 */
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;
import { formData } from "DataModule";
import SceneContent from "./ContentScene";
import { getStore, setStore } from "./Utils";
import Request from "./Request";
import { BaseUrl } from "./Data";

@ccclass
export default class Form extends cc.Component {
    static inst: Form;
    @property(cc.Node)
    form_input: cc.Node = null; // 输入框
    isInput: boolean = false; //判断用户是否输入
    // 提交数据的数据模型
    submitData: formData = {
        name: '',
        hasChild: false,
        hasParent: false
    }

    onLoad() {
        Form.inst = this;
    }

    start() {

    }

    /**
     * @description 监听输入框值的变化
     * @param text 输入框该表后获得的值
     */
    changeValue(text: string) {
        this.submitData.name = text;
    }



    // conform 点击了确认按钮 后期优化，上面的两个框至少选择一个
    confirm() {
        if (!this.name) {
            let input = this.form_input.getChildByName("input")
            cc.tween(input)
                .to(0.2, { angle: 10 })
                .to(0.2, { angle: -10 })
                .to(0.1, { angle: 5 })
                .to(0.1, { angle: -5 })
                .to(0.1, { angle: 0 })
                .start();
            let notice = this.form_input.getChildByName("notice")
            notice.active = true;
            cc.tween(notice)
                .to(0.2, { scale: 1.2 })
                .to(0.1, { scale: 0.9 })
                .to(0.05, { scale: 1.1 })
                .to(0.02, { scale: 1 })
                .delay(2)
                .to(1, { opacity: 0 })
                .call(() => {
                    notice.active = false;
                    notice.opacity = 255;
                }).start();
        } else {
            // 隐藏输入模态框&& TODO 提交数据到后端 && 将用户信息缓存起来
            this.hide()
            setStore('submitData', this.submitData, true)
            this.sendData()
            SceneContent.inst.showFlagAndShip();
            this.hideFormInput();
            SceneContent.inst.startMove(false);
        }
    }

    // 展示输入框
    showFormInput() {
        SceneContent.inst.hideApply();
        this.form_input.active = true;
    }

    // 隐藏输入框
    hideFormInput() {
        this.form_input.active = false;
    }

    // 单选框的变化
    checkBox(event: cc.Toggle, value: string) {
        this.submitData[value] = event.isChecked
    }
    sendData() {
        let userinfo = getStore('userinfo', true)
        let openId = userinfo && userinfo.openId ? userinfo.openId : null;
        let submitData = { name: this.submitData.name, hasParent: this.submitData.hasParent ? 1 : 0, hasChild: this.submitData.hasChild ? 1 : 0, openId: openId };
        Request.inst.httpPost(`${BaseUrl}/orderUser/add`, submitData, (value) => {
            // 判断是否成功
            cc.log("提交成功")
        });
    }
    show() {
        this.node.active = true;
    }
    hide() {
        this.node.active = false;
    }

    // update (dt) {}
}
